from machine import UART,Pin,ADC
from utime import sleep_ms
from BNPlayer import Player
import random
import _thread

def thread_function1():
    global sound,sound_chang
    while True:
        sound=adc.read()//400/10
        if sound!=sound_chang:
            print(sound)
            player.volume(sound)
            sound_chang=sound

number=random.randint(1,25)        
#adc = ADC(Pin(13))
#adc.atten(ADC.ATTN_11DB)
sound=0
sound_chang=0
#创建控制MP3模块用的uart
uart=UART(1,baudrate=9600,bits=8,parity=None,stop=1,tx=25,rx=26)
player = Player(uart,Pin(27),0.5)#参数序列为 uart 是否忙状态读入GPIO 音量(0.0~1.0)
sleep_ms(500)
player.playSpec(number)#播放TF卡中的序号序号不是文件名，是文件在TF卡中的存储顺序
sleep_ms(1000)#休眠500毫秒(注意各个播放相关动作之间不能紧跟，至少需要休眠500ms)
#sound=adc.read()//400/10
player.volume(0.7)

#_thread.start_new_thread(thread_function1, ())


while 1:
    state=player.isBusy()
    if state==False:
        number=random.randint(1,25)
        player.playSpec(number)
        sleep_ms(1000)
  
